package com.ysstech.kettle.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ysstech.common.entity.PageBean;
import com.ysstech.common.util.CollectionUtil;
import com.ysstech.kettle.entity.JobRecord;
import com.ysstech.kettle.entity.JobTask;
import com.ysstech.kettle.mapper.JobRecordMapper;
import com.ysstech.kettle.service.JobRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysstech.kettle.service.JobTaskService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * <p>
 * 转换执行记录表 服务实现类
 * </p>
 *
 * @author lishuangliang
 * @since 2021-06-04
 */
@Service
public class JobJobRecordServiceImpl extends ServiceImpl<JobRecordMapper, JobRecord> implements JobRecordService {

    @Autowired
    private JobTaskService jobTaskService;

    /**
     * 查询转换执行记录管理表列表信息
     *
     * @param jobRecord
     * @param pageBean
     * @return
     * @throws Exception
     */
    public PageBean<JobRecord> queryAllTransRecord(JobRecord jobRecord, PageBean pageBean) throws Exception {
        QueryWrapper<JobRecord> wrapper = this.getQueryWrapper(jobRecord);
        IPage pageBalance = this.page(new Page<>(pageBean.getPageNum(), pageBean.getPageSize()), wrapper);
        this.switchedListJobMonitor(pageBalance.getRecords());
        return new PageBean<>(pageBalance);
    }


    /**
     * 类型，机构等中文的转换
     *
     * @param list
     * @return
     * @throws Exception
     */
    public List<JobRecord> switchedListJobMonitor(List<JobRecord> list) throws Exception {
        List<JobTask> jobTaskList = jobTaskService.list();
        Map<String, String> map = new HashMap<>();
        if (!CollectionUtils.isEmpty(list)) {
            for (JobTask jobTask : jobTaskList) {
                if (null == jobTask) {
                    continue;
                }
                map.put(jobTask.getId(), jobTask.getName());
            }
        }
        for (JobRecord jobRecord : list) {
            if (null == jobRecord) {
                continue;
            }
            if (1 == jobRecord.getFtype() && map.containsKey(jobRecord.getJobId())) {
                jobRecord.setJobName(map.get(jobRecord.getJobId()));
            }
        }
        return list;
    }

    /**
     * 查询条件
     *
     * @param jobRecord
     * @return
     * @throws Exception
     */
    private QueryWrapper<JobRecord> getQueryWrapper(JobRecord jobRecord) throws Exception {
        QueryWrapper<JobRecord> wrapper = new QueryWrapper<>();
        if (null == jobRecord) {
            return wrapper;
        }
        if (StringUtils.isNotBlank(jobRecord.getRecordTransId())) {
            wrapper.eq("RECORD_TRANS_ID", jobRecord.getRecordTransId());
        }
        wrapper.orderByDesc("START_TIME");
        return wrapper;
    }

}
